package com.ccit.service.impl;

import com.ccit.dao.EmployeeDao;
import com.ccit.domain.Employee;
import com.ccit.domain.PageBean;
import com.ccit.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao employeeDao;

    public boolean add(Employee employee) {
        return employeeDao.add(employee) > 0;
    }

    @Override
    public Employee getById(Integer id) {
        return employeeDao.getById(id);
    }

    @Override
    public boolean delete(Integer id) {
        employeeDao.deleteAttendance(id);
        return employeeDao.delete(id) > 0;
    }

    @Override
    public boolean update(Employee employee) {
        employeeDao.update(employee) ;
        employeeDao.updateAttendance(employee) ;
        employeeDao.updatePunch(employee) ;
        return  employeeDao.updateApproval(employee) > 0;
    }



    @Override
    public List<Employee> selectAll() {
        return employeeDao.selectAll();
    }

    @Override
    public PageBean<Employee> selectByPage(int currentPage, int pageSize) {
        //计算开始索引
        int begin = (currentPage-1 ) * pageSize;
        //计算查询条目数
        int size = pageSize;
        //查询数据记录
        List<Employee> employees = employeeDao.selectByPage(begin, size);
        //查询总记录数
        int totalCount = employeeDao.selectTotalCount();
        //封装pageBean对象
        PageBean<Employee> pageBean = new PageBean<>();
        pageBean.setRows(employees);  //rows
        pageBean.setTotalCount(totalCount);
        return pageBean;
    }

    @Override
    public boolean deleteByIds(Integer[] ids) {
        return employeeDao.deleteByIds(ids) > 0;
    }

}
